<template>
	<view class="content" v-if="show==1">
		<view class="yhq" v-for="(y,index) in youhuiquan">
		    <view class="yhq_l">
			  <image src="../../static/yhq_left.png" mode='scaleToFill'></image>
		      <view class="zhekou">￥{{y.money}}</view>
		    </view>
		    <view class="yhq_r">
		      <view class="tiaojian">满{{y.total}}元可用</view>
		      <view class="shijian">
		        <view class="time">
		        {{y.stime}}至{{y.etime}}可用
				<view class="anniu" @click="lingqu" :data-index="index" v-if="y.lq==0">领取</view>
				<view class="anniu2" v-if="y.lq==1">已领</view>
		        </view>
		      </view>
		    </view>
		</view>
		<view class="kong" v-if="!youhuiquan[0]">
			<image src="../../static/empty.png" mode="widthFix"/>
			<view>暂无优惠券信息</view>
		</view>
	</view>
</template>

<script>
	var app=getApp();
	export default {
		data() {
			return {
				uid: 0,
				show:0,
				youhuiquan:[]
			}
		},
		onLoad() {
			const uid = uni.getStorageSync('uid');
			if(app.isBlank(uid)==false){
				this.uid=uid;
			}
			
			this.getdata();
		},
		methods: {
			getdata(){
				var that=this;
				uni.showLoading({
				    title: '数据加载中'
				});
				uni.request({
				    url: app.globalData.url+"index/yhq",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {uid:that.uid},
				    success: (res) => {
						uni.hideLoading();
						that.youhuiquan=res.data.data.yhq
						that.show=1;
				    }
				});
			},
			yong(){
				uni.switchTab({
				    url: '/pages/goods/index'
				});
			},
			lingqu(e){
				const uid = uni.getStorageSync('uid');
				if(app.isBlank(uid)==true){
					uni.showModal({
					    content: '请先进行登录',
						confirmText:'去登录',
					    success: function (res) {
					        if (res.confirm) {
					            uni.navigateTo({
					                url: '/pages/login/login'
					            });
					        } else if (res.cancel) {
					            console.log('用户点击取消');
					        }
					    }
					});
					return false;
				}
				var index=e.currentTarget.dataset.index;
				var id=this.youhuiquan[index].id
				var that=this;
				uni.showLoading({
				    title: '领取中'
				});
				uni.request({
				    url: app.globalData.url+"index/lqyhq",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {uid:uid,id:id},
				    success: (res) => {
						uni.hideLoading();
						if(res.data.code!=1){
							uni.showToast({
							    title: res.data.msg,
								icon:'none',
							    duration: 1500
							});
							return false;
						}
						uni.showToast({
						    title: res.data.msg,
						    duration: 1000
						});
						that.youhuiquan[index].lq=1;
						
				    }
				});
			}
		}
	}
</script>

<style>
	.zhuang{width:60px;height:60px;position: absolute;right: 10px;z-index: 9999;}
	.kong view{text-align: center;color:#999;margin-top: 5px;}
	.kong image{width:100px;}
	.kong{width:100%;margin-top: 30%;text-align: center;}
	page{background-color: rgb(245, 245, 245);}
	.qiehuan{width:100%;height:50px;background-color: #fff;overflow: hidden;}
	.status{width:33.33%;float: left;line-height: 50px;font-size: 0.9rem;color:#666;text-align: center;}
	.active{color:#05c160!important;}
	.yhq{width:94%;margin: 0px 3%;overflow: hidden;padding-top: 13px;position: relative;}
	.yhq_l{width:30%;float: left;height:70px;overflow: hidden;position: relative;}
	.yhq_l image{width:100%;height:70px;}
	.zhekou{width:100%;height:70px;position: absolute;top:0px;text-align: center;line-height: 70px;color:#fff;font-size: 1.1rem;}
	.yhq_r{width:70%;float: left;height:70px;background-color: #fff;}
	.tiaojian{width:94%;margin: 0px 3%;height:39px;line-height: 39px;color:#666;border-bottom: 1px rgb(245, 245, 245) solid;}
	.shijian{width:94%;margin: 0px 3%;height:30px;overflow: hidden;}
	.time{width:100%;float: left;height:30px;line-height: 30px;overflow: hidden;color:#999;font-size: 0.6rem;position: relative;}
	.anniu{width:50px;height:22px;line-height: 22px;background-color: #05c160;text-align: center;color:#fff;font-size: 0.7rem;position: absolute;right:0px;top:4px;border-radius: 20px;}
	.anniu2{width:50px;height:22px;line-height: 22px;background-color: #9e9d9e;text-align: center;color:#fff;font-size: 0.7rem;position: absolute;right:0px;top:4px;border-radius: 20px;}
	.hover{background-color: #9e9d9e!important;}
	.bottom{width:100%;height:40px;}
</style>
